home *** CD-ROM | disk | FTP | other *** search
/ CD Actual Thematic 7: Programming / CDAT7.iso / demos / VisualAge for Java 2.0 Entry / setup / data1.cab / ide-e / IDE / cache / P76R3V (.txt) < prev    next >
Encoding:
Java Class File  |  1998-09-16  |  5.1 KB  |  127 lines

  1. package com.sun.java.swing.plaf.metal;
  2.  
  3. import com.sun.java.swing.JComponent;
  4. import com.sun.java.swing.UIManager;
  5. import com.sun.java.swing.plaf.ComponentUI;
  6. import com.sun.java.swing.plaf.basic.AbstractTreeUI;
  7. import com.sun.java.swing.plaf.basic.BasicTreeUI;
  8. import com.sun.java.swing.plaf.basic.LargeTreeModelNode;
  9. import com.sun.java.swing.plaf.basic.VisibleTreeNode;
  10. import com.sun.java.swing.tree.DefaultMutableTreeNode;
  11. import java.awt.Color;
  12. import java.awt.Graphics;
  13. import java.awt.Rectangle;
  14. import java.beans.PropertyChangeListener;
  15.  
  16. public class MetalTreeUI extends BasicTreeUI {
  17.    private static Color lineColor;
  18.    private static final String LINE_STYLE = "JTree.lineStyle";
  19.    private static final String LEG_LINE_STYLE_STRING = "Angled";
  20.    private static final String HORIZ_STYLE_STRING = "Horizontal";
  21.    private static final String NO_STYLE_STRING = "None";
  22.    private static final int LEG_LINE_STYLE = 2;
  23.    private static final int HORIZ_LINE_STYLE = 1;
  24.    private static final int NO_LINE_STYLE = 0;
  25.    private int lineStyle = 1;
  26.    private PropertyChangeListener lineStyleListener = new LineListener(this);
  27.  
  28.    protected boolean clickedInExpandControl(VisibleTreeNode node, LargeTreeModelNode eNode, int row, int rowLevel, int mouseX, int mouseY) {
  29.       if (node != null && node.isLeaf()) {
  30.          return false;
  31.       } else {
  32.          int boxWidth;
  33.          if (((BasicTreeUI)this).getExpandedIcon() != null) {
  34.             boxWidth = ((BasicTreeUI)this).getExpandedIcon().getIconWidth() + 6;
  35.          } else {
  36.             boxWidth = 8;
  37.          }
  38.  
  39.          int boxLeftX;
  40.          if (((AbstractTreeUI)this).getShowsRootHandles()) {
  41.             boxLeftX = rowLevel * super.totalChildIndent + ((BasicTreeUI)this).getLeftChildIndent() - boxWidth / 2;
  42.          } else {
  43.             boxLeftX = (rowLevel - 1) * super.totalChildIndent + ((BasicTreeUI)this).getLeftChildIndent() - boxWidth / 2;
  44.          }
  45.  
  46.          int boxRightX = boxLeftX + boxWidth;
  47.          return mouseX >= boxLeftX && mouseX <= boxRightX;
  48.       }
  49.    }
  50.  
  51.    public static ComponentUI createUI(JComponent x) {
  52.       return new MetalTreeUI();
  53.    }
  54.  
  55.    protected void decodeLineStyle(Object lineStyleFlag) {
  56.       if (lineStyleFlag != null && !lineStyleFlag.equals("Horizontal")) {
  57.          if (lineStyleFlag.equals("Angled")) {
  58.             this.lineStyle = 2;
  59.          } else if (lineStyleFlag.equals("None")) {
  60.             this.lineStyle = 0;
  61.          }
  62.       } else {
  63.          this.lineStyle = 1;
  64.       }
  65.  
  66.    }
  67.  
  68.    public void drawHorizontalPartOfLeg(Graphics g, JComponent c, int lineY, int leftX, int rightX) {
  69.       if (this.lineStyle == 2) {
  70.          super.drawHorizontalPartOfLeg(g, c, lineY, leftX, rightX);
  71.       }
  72.  
  73.    }
  74.  
  75.    public void drawVerticalPartOfLeg(Graphics g, JComponent c, int depth, int parentY, int childY, int parentRowHeight, int childRowHeight) {
  76.       if (this.lineStyle == 2) {
  77.          super.drawVerticalPartOfLeg(g, c, depth, parentY, childY, parentRowHeight, childRowHeight);
  78.       }
  79.  
  80.    }
  81.  
  82.    protected int getHorizontalLegBuffer() {
  83.       return 4;
  84.    }
  85.  
  86.    public void installUI(JComponent c) {
  87.       super.installUI(c);
  88.       if (!super.tree.isLargeModel()) {
  89.          ((AbstractTreeUI)this).setRowHeight(0);
  90.       }
  91.  
  92.       lineColor = UIManager.getColor("Tree.line");
  93.       Object lineStyleFlag = c.getClientProperty("JTree.lineStyle");
  94.       this.decodeLineStyle(lineStyleFlag);
  95.       c.addPropertyChangeListener(this.lineStyleListener);
  96.    }
  97.  
  98.    public void paint(Graphics g, JComponent c) {
  99.       super.paint(g, c);
  100.       if (this.lineStyle == 1 && !super.largeModel) {
  101.          this.paintHorizontalSeparators(g, c);
  102.       }
  103.  
  104.    }
  105.  
  106.    protected void paintHorizontalSeparators(Graphics g, JComponent c) {
  107.       g.setColor(lineColor);
  108.       Rectangle clipBounds = g.getClipBounds();
  109.       int beginRow = ((AbstractTreeUI)this).getRowContainingYLocation(clipBounds.y);
  110.       int endRow = ((AbstractTreeUI)this).getRowContainingYLocation(clipBounds.y + (clipBounds.height - 1));
  111.       if (beginRow > -1 && endRow > -1) {
  112.          for(int i = beginRow; i <= endRow; ++i) {
  113.             VisibleTreeNode node = ((AbstractTreeUI)this).getNode(i);
  114.             if (((DefaultMutableTreeNode)node).getParent() == ((DefaultMutableTreeNode)node).getRoot()) {
  115.                g.drawLine(clipBounds.x, ((BasicTreeUI)this).getNodeY(node), clipBounds.x + clipBounds.width, ((BasicTreeUI)this).getNodeY(node));
  116.             }
  117.          }
  118.  
  119.       }
  120.    }
  121.  
  122.    public void uninstallUI(JComponent c) {
  123.       c.removePropertyChangeListener(this.lineStyleListener);
  124.       super.uninstallUI(c);
  125.    }
  126. }
  127.